package it.blueocean.acanto.taglibrary.jsp.taglib.tags.dataset.filter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.AbstractComponentStateBean;

public class DatasetFilterStateBean extends AbstractComponentStateBean implements DatasetFilter
{
	private boolean orModeOn_ = false;

	private boolean notModeOn_ = false;

	private List conditions_ = new ArrayList();

	public String toString()
	{
		StringBuffer localStringBuffer = new StringBuffer();
		if(this.notModeOn_)
			localStringBuffer.append("NOT ");
		localStringBuffer.append('(');
		int i = 0;
		for (int j = 0; j < this.conditions_.size(); ++j)
		{
			DatasetFilter localDatasetFilter = (DatasetFilter) this.conditions_.get(j);
			if((localDatasetFilter.getFilterString() == null) || (localDatasetFilter.getFilterString().equals("")))
				continue;
			if(i != 0)
				localStringBuffer.append((this.orModeOn_) ? " OR " : " AND ");
			localStringBuffer.append(localDatasetFilter.getFilterString());
			i = 1;
		}
		localStringBuffer.append(')');
		return ((i != 0) ? localStringBuffer.toString() : "");
	}

	public String getFilterString()
	{
		return toString();
	}

	public List getFilterParams()
	{
		ArrayList localArrayList = new ArrayList();
		Iterator localIterator = this.conditions_.iterator();
		while (localIterator.hasNext())
		{
			DatasetFilter localDatasetFilter = (DatasetFilter) localIterator.next();
			localArrayList.addAll(localDatasetFilter.getFilterParams());
		}
		return localArrayList;
	}

	public List getConditions()
	{
		return this.conditions_;
	}

	public void setConditions(List paramList)
	{
		this.conditions_ = paramList;
	}

	public void addCondition(DatasetFilter paramDatasetFilter)
	{
		this.conditions_.add(paramDatasetFilter);
	}

	public boolean isNotModeOn()
	{
		return this.notModeOn_;
	}

	public void setNotModeOn(boolean paramBoolean)
	{
		this.notModeOn_ = paramBoolean;
	}

	public boolean isOrModeOn()
	{
		return this.orModeOn_;
	}

	public void setOrModeOn(boolean paramBoolean)
	{
		this.orModeOn_ = paramBoolean;
	}

	public boolean applyFilter(Object obj)
	{
		boolean flag;
		if(orModeOn_)
		{
			flag = false;
			Iterator iterator = conditions_.iterator();
			do
			{
				if(!iterator.hasNext() || flag)
					break;
				DatasetFilter datasetfilter = (DatasetFilter) iterator.next();
				if(datasetfilter.applyFilter(obj))
					flag = true;
			}
			while (true);
		}
		else
		{
			flag = true;
			Iterator iterator1 = conditions_.iterator();
			do
			{
				if(!iterator1.hasNext() || !flag)
					break;
				DatasetFilter datasetfilter1 = (DatasetFilter) iterator1.next();
				if(!datasetfilter1.applyFilter(obj))
					flag = false;
			}
			while (true);
		}
		return notModeOn_ ? !flag : flag;
	}

}
